package com.huarun.module.applyrecord.service.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.huarun.common.BaseServiceImpl;
import com.huarun.common.CommonException;
import com.huarun.common.util.Constant;
import com.huarun.common.util.HttpUtil;
import com.huarun.common.util.TokenUtil;
import com.huarun.module.applyrecord.bean.ApplyRecord;
import com.huarun.module.applyrecord.dao.ApplyRecordDao;
import com.huarun.module.applyrecord.service.IApplyRecordService;

/***
 * 
 * @author dzm
 * @创建时间 2018-10-29 16:08
 * @Description 审批
 */
@Service
public class ApplyRecordServiceImpl extends BaseServiceImpl<ApplyRecordDao, ApplyRecord> implements IApplyRecordService {

	@Resource
	private ApplyRecordDao applyRecordDao;

	@Override
	public ApplyRecord getByOperateNo(String operateNo) {
		
		return applyRecordDao. getByOperateNo(operateNo);
	}
	
	@Override
	public String getOperateStatus(String WorkOrderGuid,HttpServletRequest request)throws Exception  {
			long time = System.currentTimeMillis();
			try {
				String url=Constant.WORK_STATUS+WorkOrderGuid;
				String token=TokenUtil.createToken(url,request, time, null);
				url = url+"?appkey="
						+ Constant.MANAGE_APPKEY + "&time=" + time + "&token="
						+  token;
				String resultStr = HttpUtil.doGet(Constant.BASE_URL+url);
				JSONObject imisObj = JSONObject.parseObject(resultStr);
				String erroeCode=imisObj.getString("ErrorCode");
				if ("0".equals(erroeCode)) {
					JSONObject data=imisObj.getJSONObject("Data");
					JSONObject info=data.getJSONObject("DetailData");
					if (null==info) {
						return null;
					}else {
						return (String)info.get("WorkOrderStateDesc");
					}
				}else {
					throw new CommonException("营运平台调用异常：errorCode"+erroeCode);
					
				}			
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}


}
